package jk.wrapper.common.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.MissingResourceException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

public class PropertySet {
	protected static final Logger log = Logger.getLogger(PropertySet.class);
	protected static Map<String, PropertySet> instances = new HashMap<String, PropertySet>();
	protected ResourceBundle props;

	private PropertySet(String propsFile) {
		try {
			InputStream is = new FileInputStream(propsFile);
			props = new PropertyResourceBundle(is);
		} catch (FileNotFoundException e) {
			log.error("Tool filter properties FileNotFoundException", e);
		} catch (IOException e) {
			log.error("Tool filter properties IOException e", e);
		}
	}

	public synchronized static PropertySet getInstance(String propsFile) {
		if (instances.get(propsFile) == null) {
			instances.put(propsFile, new PropertySet(propsFile));
		}
		return instances.get(propsFile);
	}

	public Map<String, Class> getPropertiesMapping() {
		Map<String, Class> map = new LinkedHashMap<String, Class>();
		
		Enumeration<String> enu = props.getKeys();
		while (enu.hasMoreElements()) {
			String key = enu.nextElement();
			Class clazz = this.getClass(this.getString(key, ""), null);
			if (clazz == null)
				continue;
			map.put(key, clazz);
		}
		return map;
	}
	
	public Class getClass(String name, Class defaultValue) {
        String className = null;
        if ( defaultValue == null ) {
          className = getString(name, name);
        } else {
          className = getString(name, defaultValue.getName());
        }

        Class retClass = null;

        try {
            retClass = Class.forName(className);
        } catch (ClassNotFoundException e) {
            retClass = defaultValue;
        }

        return retClass;

    }

	public String getString(String name, String defaultValue) {
		String value = null;
		try {
			value = getProperty(name);
		} catch (MissingResourceException e) {
			value = defaultValue;
		}
		return value;
	}

	public boolean getBoolean(String name, boolean defaultValue) {
		String stringValue = getString(name, "" + defaultValue);
		boolean value = new Boolean(stringValue).booleanValue();
		return value;
	}

	public int getInteger(String name, int defaultValue) {
		String stringValue = getString(name, "" + defaultValue);

		int value = 0;
		try {
			value = Integer.parseInt(stringValue);
		} catch (NumberFormatException e) {
			value = defaultValue;
		}
		return value;
	}

	protected String getProperty(String name) throws MissingResourceException {
		if (props == null) {
			throw new MissingResourceException(name, name, name);
		} else {
			return props.getString(name);
		}
	}
	
	public static void main(String[] args) {
		PropertySet props = PropertySet.getInstance("config/tools/filter.properties");
		System.out.println(props.getPropertiesMapping());
	}
}
